package com.yin.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class Demo01 {

    // 异步调用 Ajax
    // 异步执行
    // 成功回调
    // 失败回调
    //    CompletableFuture
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 发起一个请求
        // 没有返回值的异步回调
//        CompletableFuture<Void> stringCompletableFuture = CompletableFuture.runAsync(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(Thread.currentThread().getName() + "runAsync => void");
//        });
//        System.out.println("1111");
//        stringCompletableFuture.get();// 阻塞获取执行结果
        CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "supplAsync => Integer");
            int i = 10 / 0;
            return 1024;
        });

        System.out.println(uCompletableFuture.whenComplete((t, u) -> {
            System.out.println("t" + t);
            System.out.println("u" + u);
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 404;
        }).get());


    }
}
